const Mock = require('mockjs')
const { list: listMovieLevels,find: findMovieLevel } = require('./movie-level')

const movieLevels = listMovieLevels(null)
let Movies = []
let id = 1
for (let i = 0; i < 26; ++i) {
  const minP = (i % 5 + 1)
  Movies.push(Mock.mock({
    id: id++,
    movieInstanceId: id++,
    name: '我的剧本' + i,
    author: Mock.Random.cname(),
    cover: '',
    version: '1.0.1',
    tag: ['剧情', '恐怖'],
    brief: ['简单介绍一下'],
    minPlayers: minP,
    maxPlayers: minP + (i % 6 + 3),
    publishTime: Mock.Random.datetime(),
    playTimes: Mock.Random.integer(1, 100),
    duration: Mock.Random.float(1, 10, 0.1, 0.5),
    status: Mock.Random.integer(0, 1),
    movieLevelId: movieLevels.data[i % movieLevels.data.length].id,
    fileDeleted: Mock.Random.integer(0, 1)
  }))
}

function list(page, limit, name, author, tag, movieLevelId, minPlayers, maxPlayers, minDuration, maxDuration) {
  limit = Math.max(1, Math.min(parseInt(limit), 50))
  page = Math.max(parseInt(page), 1)
  minPlayers = minPlayers ? parseInt(minPlayers) : null
  maxPlayers = maxPlayers ? parseInt(maxPlayers) : null
  minDuration = minDuration ? parseInt(minDuration) : null
  maxDuration = maxDuration ? parseInt(maxDuration) : null
  let mocks = Movies.filter(item => {
    if (minPlayers && item.minPlayers < minPlayers && item.maxPlayers < minPlayers) return false
    if (maxPlayers && item.minPlayers > maxPlayers && item.maxPlayers > maxPlayers) return false
    if (minDuration && item.duration < minDuration) return false
    if (maxDuration && item.duration > maxDuration) return false
    if (author && !item.author && item.author.indexOf(author) === -1) return false
    if (tag && item.tag && item.tag.indexOf(tag) === -1) return false
    if (movieLevelId && parseInt(movieLevelId) !== item.movieLevelId) return false
    return !(name && (!item.name || item.name.indexOf(name) === -1))
  })
  const total = mocks.length
  mocks = mocks.filter((u, index) => index < limit * page && index >= limit * (page - 1))
  mocks = mocks.map(item => {
    const lvl = findMovieLevel(item.movieLevelId)
    if (lvl) {
      item.price = lvl.price
      item.openPrice = lvl.openPrice
      item.movieLevelName = lvl.name
    }
    return item
  })
  return { success: true, data: { total: total, records: mocks }}
}

function movieVersionList(page, limit, name, author, tag, movieLevelId, fileDeleted) {
  limit = Math.max(1, Math.min(parseInt(limit), 50))
  page = Math.max(parseInt(page), 1)
  let mocks = Movies.filter(item => {
    if (author && !item.author && item.author.indexOf(author) === -1) return false
    if (tag && item.tag && item.tag.indexOf(tag) === -1) return false
    if (movieLevelId && parseInt(movieLevelId) !== item.movieLevelId) return false
    if (fileDeleted && parseInt(fileDeleted) !== item.fileDeleted) return false
    return !(name && (!item.name || item.name.indexOf(name) === -1))
  })
  const total = mocks.length
  mocks = mocks.filter((u, index) => index < limit * page && index >= limit * (page - 1))
  mocks = mocks.map(item => {
    const lvl = findMovieLevel(item.movieLevelId)
    if (lvl) {
      item.price = lvl.price
      item.openPrice = lvl.openPrice
      item.movieLevelName = lvl.name
    }
    return item
  })
  return { success: true, data: { total: total, records: mocks }}
}

function find(id) {
  id = id ? parseInt(id) : null
  for (let i = 0; i < Movies.length; ++i) {
    if (Movies[i].id === id) {
      return Movies[i]
    }
  }
  return null
}

function findByMovieInstanceId(instanceId) {
  instanceId = instanceId ? parseInt(instanceId) : null
  for (let i = 0; i < Movies.length; ++i) {
    if (Movies[i].movieInstanceId === instanceId) {
      return Movies[i]
    }
  }
  return null
}

function deleteRecord(id) {
  id = id ? parseInt(id) : null
  Movies = Movies.filter(item => item.id !== id)
}

function deleteByMovieInstanceId(instanceId) {
  instanceId = instanceId ? parseInt(instanceId) : null
  Movies = Movies.filter(item => item.movieInstanceId !== instanceId)
}

module.exports = { list, find, findByMovieInstanceId, deleteRecord, deleteByMovieInstanceId, movieVersionList }
